package zeichnen;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.Vector;

import javax.swing.JPanel;

public class PaintPanel extends JPanel{
	private Color fFarbe, zFarbe;
	private boolean objFuellen = false;
	private int mode = -1, anz, polyCounter=-1;
	
	Vector<Paintable> objList = new Vector<Paintable>();
	
	//****** aktuelle ZeichenObjekte
	MyLine aktLinie ;
	MyPolygon aktPolygon ;
	MyOblong aktRechteck ;
	MyCircuit aktKreis ;
	
	//****** Konstuktor
	public PaintPanel(){
		this.setBackground(Color.white);
		this.setFColor(Color.white);
		this.setZColor(Color.black);
		this.setFillmode(false);
		this.setMode(-1);
		
		//Zuordnung der Mouse- und MouseMotionListener
		this.addMouseListener(new MouseAdapter(){
			public void mousePressed(MouseEvent arg0) {
				m_pressed(arg0);	//*** druecken
			}
			public void mouseReleased(MouseEvent arg0) {
				m_released(arg0);	//*** los lassen	
			}	
		});
		this.addMouseMotionListener(new MouseMotionAdapter(){
			public void mouseDragged(MouseEvent arg0) {
				m_dragged(arg0);	//*** ziehen
			}
		});
	}
	
	protected void m_dragged(MouseEvent maus) {		//*** ziehen
//		System.out.println("drapped");
		switch (mode){
	/*	case 0:
//			System.out.println("dragged - Polygon");
			aktPolygon.setP2(maus.getPoint());
			break;	*/
		case 2:
//			System.out.println("dragged - Rechteck");
			aktRechteck.setP2(maus.getPoint());
//			objList.add(aktRechteck);
			break;
		case 3:
//			System.out.println("dragged - Kreis");
			aktKreis.setP2(maus.getPoint());
			break;
		}
		repaint();
	}

	protected void m_released(MouseEvent maus) {		//*** los lassen
		switch (mode){
	/*	case 0:
			System.out.println("released _ Polygon");
			aktPolygon.setP2(maus.getPoint());
			objList.add(aktPolygon);
			break; 	*/
		case 2:
			System.out.println("released _ Rechteck");
			aktRechteck.setP2(maus.getPoint());
			objList.add(aktRechteck);
			break;
		case 3:
			System.out.println("released _ Kreis");
			aktKreis.setP2(maus.getPoint());
			objList.add(aktKreis);
			break;
		}
		repaint();
	}

	protected void m_pressed(MouseEvent maus) {		//*** druecken
		
		switch (mode){
		case 0: 
			
			if(polyCounter == -1){
				System.out.println("pressed - Polygon Start");
				aktPolygon = new MyPolygon(maus.getPoint() ,zFarbe, anz);
				polyCounter++;
				aktPolygon.addPiont(maus.getPoint(), polyCounter);
				polyCounter++;
			} else{ 
				if(polyCounter < this.anz-1){
					System.out.println("pressed - Polygon "+polyCounter);
					aktPolygon.addPiont(maus.getPoint(), polyCounter);
					polyCounter++;
				}else{
					System.out.println("pressed - Polygon End "+polyCounter);
					aktPolygon.addPiont(maus.getPoint(), polyCounter);
					polyCounter = -1;
					objList.add(aktPolygon);
				}
			}
			repaint();
			break;
		
		case 2:
			System.out.println("pressed - Rechteck");
			aktRechteck = new MyOblong(maus.getPoint(), maus.getPoint(), fFarbe, zFarbe, objFuellen);
			break;
		case 3:
			System.out.println("pressed _ Kreis");
			aktKreis = new MyCircuit(maus.getPoint(), maus.getPoint(), fFarbe, zFarbe, objFuellen);
			break;
		}
		
	}

	public void paintComponent(Graphics grafik){
		super.paintComponent(grafik);
		Graphics2D grafik2D = (Graphics2D) grafik;
		for (Paintable zeichenBrett : objList){
			zeichenBrett.painting(grafik2D, false);
		}
		switch (mode){
		case 0 :
			if(aktPolygon != null)
				aktPolygon.painting(grafik2D, true);break;
		
		case 2 :
			aktRechteck.painting(grafik2D, true);break;
		case 3 :
			aktKreis.painting(grafik2D, true);break;
		}
	}

	public void setFColor(Color farbe) {
		this.fFarbe = farbe;
	}

	public void setZColor(Color farbe) {
		this.zFarbe = farbe;
	}

	public void setFillmode(boolean selected) {
		this.objFuellen = selected;
	}

	public void setMode(int mode) {
		this.mode = mode;
	}

	public void setMode(int mode, int anz) {
		this.mode = mode;
		this.anz = anz;
	}
	
	

}
